<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-button type="success" @click="showAddCate">添加分类</el-button>
      <tree-table
       :data = "goodsList"
       :columns = "columns"
       show-index
       index-text ="#"
       :expand-type= "false"
       :selection-type= "false"
       border
      >
      <!-- 是否有效插槽 -->
      <template slot="isdel" slot-scope="scope" >
        <i v-if="scope.row.cat_deleted === false" class="el-icon-success" style="color:green;"></i>
        <i v-else class="el-icon-error" style="color:red;"></i>
      </template>
      <!-- 等级插槽 -->
      <template slot="level" slot-scope="scope">
        <el-tag v-if="scope.row.cat_level=== 0">一级</el-tag>
        <el-tag v-else-if="scope.row.cat_level=== 1" type="success">二级</el-tag>
        <el-tag v-else type="warning">三级</el-tag>
      </template>
      <!-- 操作插槽 -->
      <template slot="set">
        <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
         <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
      </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryData.pagenum"
        :page-sizes="[5, 10, 15, 25]"
        :page-size="queryData.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="50%"
      @close="removeAddDialog"
      >
      <el-form ref="cateFormRef" :model="cateFormData" :rules="cateFormRules" label-width="80px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="cateFormData.cat_name" ></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
           <el-cascader
             v-model="cascaderVal"
             :options="cateData"
             :props="cascaderProps"
             @change="addCateHandleChange">
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryData: {
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表
      goodsList: [],
      // 商品总数
      total: 0,
      // 配置tree-table组件
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          //  开启自定义插槽
          type: 'template',
          template: 'isdel'
        },
        {
          label: '等级',
          type: 'template',
          template: 'level'
        },
        {
          label: '操作',
          type: 'template',
          template: 'set'
        }
      ],
      // 添加分类对话框Flag
      addDialogVisible: false,
      // 添加分类数据
      cateFormData: {
        cat_name: '',
        cat_level: 0,
        cat_pid: 0
      },
      // 添加分类数据验证规则
      cateFormRules: {
        cat_name: [
          { required: true, message: '请输分类名称', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ]
      },
      // 添加分类获取的数据
      cateData: [],
      // cascader组件 v-model的数据  v-model: 被选中的数据, 一般指定的是id
      cascaderVal: [],
      // 配置cascader
      cascaderProps: {
        expandTrigger: 'hover',
        // 级联选择器所需的数据
        // 指定options数据中, label 要渲染到页面的字段名
        label: 'cat_name',
        // value: 选中项,v-model里需要的id
        value: 'cat_id',
        checkStrictly: true
      }
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$axios.get('categories', {
        params: this.queryData
      })
      this.goodsList = res.data.result
      this.total = res.data.total
    },
    // 每页几条发生变化时
    handleSizeChange (pagesize) {
      this.queryData.pagesize = pagesize
      this.getGoodsList()
    },
    // 页码发生变化时
    handleCurrentChange (pagenum) {
      this.queryData.pagenum = pagenum
      this.getGoodsList()
    },
    // 显示添加分类对话框
    async showAddCate () {
      this.addDialogVisible = true
      const { data: res } = await this.$axios.get('categories', {
        params: { type: 2 }
      })
      this.cateData = res.data
    },
    // 添加选中级联选中器事件
    async addCateHandleChange () {
      if (this.cascaderVal.length > 0) {
        const length = this.cascaderVal.length
        this.cateFormData.cat_level = length
        this.cateFormData.cat_pid = this.cascaderVal[length - 1]
      } else {
        this.cateFormData.cat_level = 0
        this.cateFormData.cat_pid = 0
      }
    },
    // 关闭添加分类对话框  清空里面的值
    removeAddDialog () {
      this.$refs.cateFormRef.resetFields()
      this.cateFormData.cat_level = 0
      this.cateFormData.cat_pid = 0
      this.cascaderVal = []
    },
    // 点击确定 验证并发起请求
    addCate () {
      this.$refs.cateFormRef.validate(async flag => {
        if (!flag) return this.$message('请填写必要项')
        const { data: res } = await this.$axios.post('categories', this.cateFormData)
        if (res.meta.status !== 201) return this.$message.error('添加失败')
        this.getGoodsList()
        this.addDialogVisible = false
        this.$message.success('添加成功')
      })
    }
  }
}
</script>

<style scoped>
.el-button {
  margin-bottom: 30px;
}
.el-cascader {
  width: 50%;
}
</style>
